#pragma once
#ifndef __NEZHACFILESYSTEM_H__
#define __NEZHACFILESYSTEM_H__

#include "NezhaIFileSystem.h"
#include "Singleton.h"
#include "tinythread.h"

namespace Nezha
{
	namespace stream
	{

		const int FILE_SYSTEM_MAX_PATH = 1024;

		class CFile
		{
		public:
			CFile(FILE* file, const char* fullName);

			~CFile();

		public:
			FILE*			mFile;
			String			mFullPath;
			const char*		mFileName;
		}; // class CFile

		class FOUNDATION_ENTRY CFileSystem: public IFileSystem,public SingletonEx<CFileSystem>
		{
			NEZHA_SINGLETON_DECLARE(CFileSystem)
			
		public:
			CFileSystem();

			virtual ~CFileSystem();

		public:

			virtual IReadFile* createReadFile(const char* filename);

			//virtual IReadFile* createMemoryReadFile(void* memory,
			//											 int len,
			//											 const char* fileName,
			//											 bool deleteMemoryWhenDropped = false);
			virtual IWriteFile* createWriteFile(const char* filename, bool append=false);

			virtual bool addZipFileArchive(const char* filename,
										   bool ignoreCase = true,
										   bool ignorePaths = true,
										   u32 poolStartSize = 1);

			virtual bool addFolderFileArchive(const char* filename,
										      bool ignoreCase = true,
											  bool ignorePaths = true);

			virtual bool addPakFileArchive(const char* filename,
										   bool ignoreCase = true,
										   bool ignorePaths = true); 

			virtual bool removeFileArchive(const char* filename);

			virtual const char* getWorkingDirectory();

			virtual bool changeWorkingDirectoryTo(const char* newDirectory);

			virtual String getAbsolutePath(const String& filename) const;

			virtual String getFileDir(const String& filename) const;

			virtual IFileList* createFileList() const;

			virtual bool existFile(const char* filename) const;

			virtual bool existFile(const String& filename) const;

			virtual void clear();

			virtual u32 releaseUnusedPoolHandles();

			virtual u32 releaseUnusedPoolHandles(const char* filename);

			static CFile* open(const char*filename, const char *attrib);

		protected:
			IReadFile* createAndOpenFileFromArchives(const char* filename);

		protected:

			static char sWorkingDirectory[FILE_SYSTEM_MAX_PATH];

			tthread::mutex mFileSystemLock;
		};

		// internal func
		IReadFile* InternalCreateReadFile(const char* fileName);

		IWriteFile* InternalCreateWriteFile(const char* fileName, bool append);
	};
};

#endif // __NEZHACFILESYSTEM_H__